package com.enterpriseDigitalPlatform.controller.renshi.dept;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class DeptDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DeptDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int addData(DeptDTO deptDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<String> placeArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (deptDTO.getName() != null) {
            keyArr.add("name");
            placeArr.add("?");
            valueArr.add(deptDTO.getName());
        }
        if (deptDTO.getPid() != null) {
            keyArr.add("pid");
            placeArr.add("?");
            valueArr.add(deptDTO.getPid());
        }
        if (deptDTO.getLevel() != null) {
            keyArr.add("level");
            placeArr.add("?");
            valueArr.add(deptDTO.getLevel());
        }
        if (!keyArr.isEmpty()) {
            String sql = "insert into renshi_dept(" + String.join(",", keyArr) + ") values(" + String.join(",", placeArr) + ")";
            return jdbcTemplate.update(sql, valueArr.toArray());
        }
        return 0;
    }

    public int editData(DeptDTO deptDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (deptDTO.getName() != null) {
            keyArr.add("name");
            valueArr.add(deptDTO.getName());
        }
        if (deptDTO.getPid() != null) {
            keyArr.add("pid");
            valueArr.add(deptDTO.getPid());
        }
        if (deptDTO.getLevel() != null) {
            keyArr.add("level");
            valueArr.add(deptDTO.getLevel());
        }
        if (deptDTO.getPname() != null) {
            keyArr.add("pname");
            valueArr.add(deptDTO.getPname());
        }
        if (deptDTO.getPids() != null) {
            keyArr.add("pids");
            valueArr.add(deptDTO.getPids());
        }
        if (!keyArr.isEmpty()) {
            String sql = "update renshi_dept set " + String.join(",", keyArr) + " where id = ? and is_del = 0";
            valueArr.add(deptDTO.getId());
            return jdbcTemplate.update(sql, valueArr.toArray());
        }
        return 0;
    }

    public int delData(DeptDTO deptDTO) {
        String sql = "update renshi_dept set is_del = 1 where id = ? and is_del = 0";
        return jdbcTemplate.update(sql, deptDTO.getId());
    }

    public List<Dept> getList(DeptDTO deptDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (deptDTO.getId() != null) {
            keyArr.add("id = ?");
            valueArr.add(deptDTO.getId());
        }
        if (deptDTO.getPid() != null) {
            keyArr.add("pid = ?");
            valueArr.add(deptDTO.getPid());
        }
        if (deptDTO.getLevel() != null) {
            keyArr.add("level = ?");
            valueArr.add(deptDTO.getLevel());
        }
        keyArr.add("is_del = 0");
        String sql = "select * from renshi_dept where " + String.join(" and ", keyArr) + " order by id desc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dept.class), valueArr.toArray());
    }
}
